package com.qinghua.blog.controller;

import com.qinghua.blog.po.Tag;
import com.qinghua.blog.service.BlogService;
import com.qinghua.blog.service.TagService;
import com.qinghua.blog.vo.BlogQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

@Controller
public class TagShowController {

    @Autowired
    private TagService tagService;

    @Autowired
    private BlogService blogService;

    @GetMapping("/tag/{id}")
    public String types(@PageableDefault(size = 10,sort = {"updateTime"},direction = Sort.Direction.DESC) Pageable pageable, @PathVariable Long id, Model model){

        //查询所有标签
        List<Tag> tagList=tagService.getTagByTop(10000);

        if(id == -1){
            //如果从导航栏点击进入，就查询类型对应最多博客
             id=tagList.get(0).getId();
        }
        for(Tag tag:tagList){
            if(tag.getId()==id){
                model.addAttribute("tag",tag.getName());
            }
        }
        model.addAttribute("tags",tagList);
        BlogQuery blogQuery=new BlogQuery();

        Page page =blogService.listBlog(id,pageable);
        model.addAttribute("page",page);
        model.addAttribute("activeTypeId",id);
        return "tags";
    }
}
